<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Model extends CI_Model{
    /**
     * @var CI_DB_active_record $db
     */
    public $db;
    protected  $time_zone = '+8:00';

    public function __construct(){
//        $this -> output -> enable_profiler(TRUE);
        parent::__construct();
        $CI =& get_instance();
        $this->db = $CI->db;
    }

    public function createVo(){
        $query = $this->db->query('SHOW COLUMNS FROM ' . $this->db->dbprefix . $this->table);
        $vo = new stdClass();
        foreach($query->result() as $c){
            $vo->{$c->Field} = '';
        }
        return $vo;
    }

    public function getOne($where=null){
        $this->db->select("*");
        $this->db->from($this->table);
        if($where != null){
            $this->db->where($where);
        }
        return $this->db->get()->row();
    }

    public function getAll($where=null,$order_by=null){
        $this->db->select("*");
        $this->db->from($this->table);
        if($where != null){
            $this->db->where($where);
        }
        if($order_by != null){
            $this->db->order_by($order_by);
        }
        return $this->db->get()->result();
    }

    public function update($vo){
        $this->db->where('id',$vo->id);
        if(property_exists($vo, 'update_time')){
            $vo->update_time = time();
        }
//		$success =  $this->db->update($this->table,$vo) && ($this->db->affected_rows() >= 1);
        $success =  $this->db->update($this->table,$vo);
        if($success === FALSE){
            $this->db->_trans_status = FALSE;
        }
        return $success;
    }

    public function add($vo){
        if(property_exists($vo,'update_time'))
            $vo->update_time = time();
        if(property_exists($vo,'create_time'))
            $vo->create_time = time();
        $result = $this->db->insert($this->table,$vo);
        if($result && (!property_exists($vo, 'id') || !$vo->id)){
            $vo->id = $this->db->insert_id();
        }
        return $vo->id;
    }
    public function save($vo){
        if(!empty($vo->id)){
            return $this->update($vo) ;
        }else{
            return $this->add($vo);
        }
    }

    public function delete($id, $update_time){
        $this->db->where('id', $id);
        $this->db->where('update_time', $update_time);
        $ret = $this->db->delete($this->table);
        if($ret || $this->db->affected_rows() >= 1){
            return true;
        }
        return false;
    }
    public function tableExists($tableName){
        $sql = "select table_name from information_schema.tables where table_name='".$tableName."' and table_schema='".$this->db->database."';";
        $ret = $this->db->query($sql)->result();
        if($ret){
            return true;
        }
        return false;
    }

    /**
     * 根据线类型格式化时间数据
     * @param $startdate
     * @param $enddate
     * @param $line_type
     * @return array
     */
    public static function getDateFromRange($startdate, $enddate, $line_type = 3)
    {
        //图标线型为日
        if ($line_type == 3) {
            $stimestamp = strtotime($startdate . ' 00:00:00');
            $etimestamp = strtotime($enddate . ' 23:59:59');
            if ($etimestamp < $stimestamp) return [];
            // 计算日期段内有多少天
            $days = (int)ceil(($etimestamp - $stimestamp) / 86400);
            // 保存每天日期
            $date = array();
            for ($i = 0; $i < $days; $i++) {
                $date[] = date('Y-m-d', $stimestamp + (86400 * $i));
            }
            return $date;
            //图标线型为月
        } else if ($line_type == 2) {
            $start = date('Y-m', strtotime($startdate));
            $end = date('Y-m', strtotime($enddate));

            $st = strtotime($start . '-01');
            $et = strtotime($end . '-01');

            $t = $st;
            $i = 0;
            while ($t <= $et) {
                $d[$i] = trim(date('Y-m', $t), ' ');
                $t += strtotime('+1 month', $t) - $t;
                $i++;
            }
            return $d;
        } else if ($line_type == 1) {

            $start = (int)date('Y', strtotime($startdate));
            $end = (int)date('Y', strtotime($enddate));
            $t = $start;
            $i = 0;
            while ($t <= $end) {
                $d[$i] = trim($t, ' ');
                $t += 1;
                $i++;
            }
            return $d;
        }

    }

    /**
     * 获取最近七天所有日期
     */
    public static function get_weeks($time = '', $format = 'Y-m-d')
    {
        $time = $time != '' ? $time : time() + 24*60*60;
        //组合数据
        $date = [];
        for ($i = 0; $i < 7; $i++) {
            $date[$i] = date($format, strtotime('+' . $i - 7 . ' days', $time)) . '';
        }
        return $date;
    }
}
